package cn.com.dhcc.app.pub.core.listener;

import javax.servlet.ServletContextEvent;

import org.apache.log4j.Logger;

import cn.com.dhcc.app.core.boot.BootParam;
import cn.com.dhcc.app.core.exception.InitException;
import cn.com.dhcc.app.core.handler.threadpoolhandler.ThreadPool;
import cn.com.dhcc.app.core.listener.AppInitListener;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;

/**
 * 启动线程池监听启动服务
 */
public class ThreadPoolMonitorListener implements AppInitListener {

	//间隔时间
	public static int  gapTime = 10*1000;
	public Thread threadPoolMonitorThread;
	private static Logger logger = Log.getLogger(LOGTYPE.THREADMONITOR);
	@Override
	public void init(ServletContextEvent contextEvent, BootParam bootParam) throws InitException {
		if(threadPoolMonitorThread==null){
			threadPoolMonitorThread = new Thread(new Runnable() {
				@Override
				public void run() {
					while(true){
						logger.info(ThreadPool.printAllPoolStatic());
						try {
							Thread.sleep(gapTime);
						} catch (InterruptedException e) {
							//
						}
					}
				}
			});
		}
		if(!threadPoolMonitorThread.isAlive()){
			threadPoolMonitorThread.start();
			contextEvent.getServletContext().log("线程池监听启动,间隔"+gapTime+" ms");
		}
	}
	@Override
	public void destroyed(ServletContextEvent contextEvent, BootParam bootParam) throws Exception {
		if(threadPoolMonitorThread!=null){
			threadPoolMonitorThread.interrupt();
		}
	}
}
